package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;

import com.moleq.db.DatabaseHelper;
import com.moleq.models.SettingSelection;

public class SelectionService {
	private DatabaseHelper DBHelper;
	public SelectionService(Context context){
		this.DBHelper = new DatabaseHelper(context);
	}
	
	public List<SettingSelection> getSelections(String Group){
		String sql = "select * from t_setting_selection where ss_group = '" + Group + "'";
		Cursor cursor = DBHelper.rawQuery(sql, null);
		int position = 0;
		List<SettingSelection> Source = new ArrayList<SettingSelection>();
		if(cursor == null){
			return Source;
		}
		while(cursor.moveToNext()){
			SettingSelection selection = new SettingSelection(
					cursor.getInt(0),
					cursor.getString(1),
					cursor.getString(2),
					cursor.getInt(3),
					cursor.getInt(4),
					position
					);
			position++;
			Source.add(selection);
		}
		cursor.close();
		return Source;
	}
	
	public void DeleteSelection(String Group,String Value){
		String errorString = "";
		String sql = "Delete from t_setting_selection where ss_group = '" + Group + "' and " + "ss_value = '" + Value + "'";
		DBHelper.execSQL(sql);
	}
	
	public String InsertSelection(String Group,String Value,int Max){
		String errorString = "";
		String sql = "Insert into t_setting_selection values(" 
				+ null + ","
				+ "'" + Group + "',"
				+ "'" + Value + "',"
				+ Max + ","
				+ 1 + ")";
		errorString = DBHelper.execSQL(sql);
		return errorString;
	}
	
	public int GetSelectionOrder(String Group){
		String sql = "select Max(ss_order) from t_setting_selection where ss_group = '" + Group + "'";
		Cursor cursor = DBHelper.rawQuery(sql, null);
		int Max = 1;
		if(cursor == null){
			return Max;
		}
		while(cursor.moveToNext()){
			Max = cursor.getInt(0);
		}
		cursor.close();
		return Max;
	}
}
